package com.xjscrm.console.service.common.user.impl;

import cn.hutool.core.bean.BeanUtil;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.entity.Merchant;
import com.xjscrm.common.entity.MerchatChildmerchatRelation;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.console.mapper.MerchantMapper;
import com.xjscrm.console.mapper.MerchatChildmerchatRelationMapper;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.user.AppPwdDto;
import com.xjscrm.console.param.user.ChangeHeadImageDto;
import com.xjscrm.console.param.user.ChildMerchatDto;
import com.xjscrm.console.param.user.DeleMerchatChildmerchatRelationDto;
import com.xjscrm.console.service.common.user.UserService;
import com.xjscrm.console.service.customer.corp.WwCorpService;
import com.xjscrm.console.service.customer.sys.SysConfigService;
import com.xjscrm.console.vo.MerchantBaseInfoVo;
import com.xjscrm.console.vo.sysconfig.SysBrandConfigVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private MerchatChildmerchatRelationMapper merchatChildmerchatRelationMapper;

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private WwCorpService wwCorpService;


    @Override
    public void getUserByPhone(String phone) {
        if (StringUtils.isBlank(phone) || phone.length()!=11){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MOBILE_ERROR);
        }
        Merchant merchant = merchantMapper.getByPhone(phone);
        if (merchant==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.USER_UNREG);
        }
    }

    @Override
    public void addMerchatChildmerchatRelation(ChildMerchatDto childMerchatDto) {
        if (StringUtils.isBlank(childMerchatDto.getUserAccount())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.USERACCOUNT_ERROR);
        }
        if(childMerchatDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        MerchatChildmerchatRelation childmerchatRelation=merchatChildmerchatRelationMapper.getByUserAccount(childMerchatDto.getUserAccount());
        if(childmerchatRelation!=null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.USERACCOUNT_EXIST);
        }

        MerchatChildmerchatRelation merchatChildmerchatRelation=new MerchatChildmerchatRelation();
        BeanUtil.copyProperties(childMerchatDto,merchatChildmerchatRelation);
        merchatChildmerchatRelationMapper.addMerchatChildmerchatRelation(merchatChildmerchatRelation);
    }

    @Override
    public MerchatChildmerchatRelation getMerchatChildmerchatRelationByUserAccount(String userAccount){
        MerchatChildmerchatRelation childmerchatRelation=merchatChildmerchatRelationMapper.getByUserAccount(userAccount);
        return childmerchatRelation;
    }

    @Override
    public MerchantBaseInfoVo getMerchantBaseInfo(BaseDto baseDto) {
        if (baseDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        Merchant merchant = merchantMapper.getById(baseDto.getMerId());
        if (merchant==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.USER_UNREG);
        }
        MerchantBaseInfoVo merchantVo=new MerchantBaseInfoVo();
        BeanUtil.copyProperties(merchant,merchantVo);
        if(StringUtils.isBlank(merchant.getPassword())){
            merchantVo.setHavePwd(0);
        }else {
            merchantVo.setHavePwd(1);
        }

        SysBrandConfigVo sysBrandConfig = sysConfigService.findSysBrandConfig(baseDto.getMerId());
        if (sysBrandConfig != null) {
            merchantVo.setSystemName(sysBrandConfig.getSystemName());
            merchantVo.setLogo(sysBrandConfig.getSystemLog());
        }
        merchantVo.setShowCorpConfig(wwCorpService.check(merchantVo.getId()) ? 0 : 1);
        return merchantVo;
    }


    @Override
    public void changeMerchantHeadImage(ChangeHeadImageDto changeMHeadImageDto) {
        if (changeMHeadImageDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if (changeMHeadImageDto.getHeadImage()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        Merchant merchant = merchantMapper.getById(changeMHeadImageDto.getMerId());
        if (merchant==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.USER_UNREG);
        }
        merchant.setHeadImage(changeMHeadImageDto.getHeadImage());
        merchantMapper.updateMerchat(merchant);
    }

    @Override
    public void updateMerchatChildmerchatRelation(HashMap<String, Object> map) {
        if(map.get("merId")==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
        if(map.get("oldAccount")==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        if(map.get("newAccount")==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        Long merId=Long.valueOf(map.get("merId").toString());
        String oldAccount=map.get("oldAccount").toString();
        String newAccount=map.get("newAccount").toString();
        merchatChildmerchatRelationMapper.deleMerchatChildmerchatRelationByAcoount(oldAccount,merId);

        MerchatChildmerchatRelation merchatChildmerchatRelation=new MerchatChildmerchatRelation();
        merchatChildmerchatRelation.setMerId(merId);
        merchatChildmerchatRelation.setUserAccount(newAccount);
        merchatChildmerchatRelationMapper.addMerchatChildmerchatRelation(merchatChildmerchatRelation);

    }

    @Override
    public void BatchDeleMerchatChildmerchatRelation(DeleMerchatChildmerchatRelationDto deleMerchatChildmerchatRelationDto) {
        for (String userAccount : deleMerchatChildmerchatRelationDto.getUserAccounts()) {
           try {
               merchatChildmerchatRelationMapper.deleMerchatChildmerchatRelationByAcoount(userAccount, deleMerchatChildmerchatRelationDto.getMerId());
           }catch (Exception e){
               e.printStackTrace();
           }
        }
    }

    @Override
    public Merchant getAppPwd(AppPwdDto appPwdDto) {
       if(StringUtils.isBlank(appPwdDto.getPhone())){
           throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,"手机号为空");
       }
        Merchant merchant = merchantMapper.getByPhone(appPwdDto.getPhone());
       if(merchant==null){
           throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,"用户不存在");
       }
        return merchant;

    }

    @Override
    public Merchant getById(BaseDto baseDto) {
        if(baseDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION,ResponseMsg.MERID_NULL);
        }
        return merchantMapper.getById(baseDto.getMerId());
    }

    @Override
    public Merchant getById(Long id) {
        return merchantMapper.getById(id);
    }
}
